define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index' + location.search,
                    add_url: 'goods/goods/add',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    import_url: 'goods/goods/import',
                    table: 'goods',
                    dragsort_url:''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.type = 1;
                    op.type = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'first_cate', title: __('First_cate'), searchList: Config.categoryList, formatter: Table.api.formatter.flag },
                        { field: 'two_cate', title: __('Two_cate'), searchList: Config.categoryList, formatter: Table.api.formatter.flag },
                        { field: 'tags', title: __('Tags'), operate: 'LIKE', formatter: Table.api.formatter.flag },
                        // { field: 'type', title: __('类型'), searchList: { "1": __('普通商品'), "2": __('区域商品') }, formatter: Table.api.formatter.normal },
                        { field: 'title', title: __('Title'), operate: 'LIKE' },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images },
                        { field: 'status', title: __('Status'), searchList: { "normal": __('Normal'), "hidden": __('Hidden') }, formatter: Table.api.formatter.status },
                        { field: 'yue', title: __('Yue'), searchList: { "1": __('Yes'), "0": __('No') }, formatter: Table.api.formatter.normal },
                        { field: 'pay_auth', title: __('购买权限'), searchList: { "1": __('Yes'), "0": __('No') }, formatter: Table.api.formatter.normal },
                        { field: 'hot', title: __('首页推荐'), searchList: { "1": __('Yes'), "0": __('No') }, formatter: Table.api.formatter.normal },
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                // {
                                //     name: 'setregion',
                                //     title: '区域',
                                //     text: '区域',
                                //     extend: 'data-area=["90%","90%"]',
                                //     classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                //     url: 'goods/region/index',//接口地址 控制器名/方法名
                                //     visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                //         if (row.type == '2') {
                                //             return true;//显示
                                //         } else {
                                //             return false;//隐藏
                                //         }
                                //     },
                                // },
                                {
                                    name: 'giveaway',
                                    title: '分佣配置',
                                    text: '分佣配置',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'goods/goods/giveaway',//接口地址 控制器名/方法名
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'goods/goods/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'title', title: __('Title'), align: 'left' },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'goods/goods/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'goods/goods/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        giveaway: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
